package ee.ut.math.chess.base.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

//a position on the chessboard e.g. a1 is (0;0)
public class Position implements Serializable {
	private static final long serialVersionUID = 1L;
	private static Map<String, Integer> columns = new HashMap<String, Integer>();
	static {
		columns.put("a", 0);
		columns.put("b", 1);
		columns.put("c", 2);
		columns.put("d", 3);
		columns.put("e", 4);
		columns.put("f", 5);
		columns.put("g", 6);
		columns.put("h", 7);
	}
	private int x;
	private int y;

	public Position(String position) {
		this.x = Integer.valueOf(position.substring(1, 2)) - 1;
		this.y = columns.get(position.substring(0, 1));
	}

	public Position(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Position) {
			Position pos2 = (Position) obj;
			return x == pos2.x && y == pos2.y;
		}
		return false;
	}

	@Override
	public String toString() {
		for (Entry<String, Integer> entry : columns.entrySet()) {
			if (entry.getValue() == y) {
				return entry.getKey() + (x + 1);
			}
		}
		return null;
	}

}
